<template>
  <div class="main">
    <div class="forms">
      <div class="title">雷培德IOT设备控制系统</div>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="账号" prop="username">
          <el-input
            type="text"
            placeholder="请输入账号"
            v-model="ruleForm.username"
            maxlength="20"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            placeholder="请输入密码"
            v-model="ruleForm.password"
            maxlength="30"
            show-password
          ></el-input>
          <!-- <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input> -->
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- <div id="app">
    <p>{{ $t("message.hello") }}</p>
    <button @click="changeLanguage('en')">English</button>
    <button @click="changeLanguage('zh')">中文</button>
  </div> -->
  <div class="beian2">
      <span>IOT设备控制</span>
     
    </div>
    <div class="beian">
      <a href="https://beian.miit.gov.cn/" target="_blank">浙ICP备2023004903号-2</a>
    </div>
  </div>
</template>

<script>
import { ulogin, userRouter } from "../../request/api";
import { successmsg, failmsg } from "../../tools/tools";

export default {
  data() {
    // 密码
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    // 账号
    var validateUser = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入账号"));
      } else {
        callback();
      }
    };
    return {
      // 用户信息
      userInfo: {},
      // 输入信息
      ruleForm: {
        username: "",
        password: "" 
      },
      rules: {
        username: [{ validator: validateUser, trigger: "blur" }],
        password: [{ validator: validatePass, trigger: "blur" }]
      }
    };
  },
  created() {
    this.tokenFail();
  },
  mounted() {},
  methods: {
    changeLanguage(lang) {
      this.$i18n.locale = lang;
    },
    // token失效
    tokenFail() {
      if (window.localStorage.tokenFail == "true") {
        failmsg("用户状态失效，请重新登录");
      }
      window.localStorage.setItem("tokenFail", false);
    },
    // 登录
    submitForm(formName) {

      this.$refs[formName].validate(valid => {
        if (valid) {
          //有值
          this.userInfo = this.ruleForm;
          ulogin(this.userInfo)
            .then(d => {
              if (d.code == "0000") {
                // 把用户信息存储到localStorage中
                localStorage["userInfo"] = JSON.stringify(d.data);
                
                // this.getPermissions();
                //  跳转到主页 地图
                this.$router.push("/controller");
                // 显示登录成功
                successmsg("登录成功");
              }
            })
            .catch(e => {
              console.log("接口调用失败");
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
.beian2 {
  color: #fff;
  position: fixed;
  bottom: 80px;
  left: 47vw;
}
/* 备案号 */
.beian {
  position: fixed;
  bottom: 100px;
  left: 45vw;
}
.beian a {
  color: white;
}
.title {
  margin: 20px 0px 20px 80px;
  text-align: center;
  font-size: 24px;
}
.el-button {
  width: 200px;
  margin-left: 50px;
}
.main {
  width: 100vw;
  height: 100vh;
  background: url("../../assets/image/login-bg-dark.jpg");
  background-repeat: no-repeat;
  background-size: cover;
  /* background: linear-gradient(to right,#87CEFA,#7eb7f1)!important; */
}
.main .forms {
  width: 400px;
  height: 300px;
  background: #fff;

  /* background: #F5F5DC; */
  padding-right: 70px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border-radius: 2%;
}
</style>
